﻿using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class FireReportOutBLL
    {
        #region 构造单例
        private FireReportOutBLL() { }
        private static FireReportOutBLL _instance;
        public static FireReportOutBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new FireReportOutBLL());
            }
        }
        #endregion


        public ResultDto<List<FireMQTTResponseModel>> Add(Firereportout info)
        {
            ResultDto<List<FireMQTTResponseModel>> result = new ResultDto<List<FireMQTTResponseModel>>();
            var fireInfo = BLL.FireInfoBLL.Instance.Get(info.FireId);
            if (fireInfo == null)
            {
                result.Status = -1;
                result.Message = "FireInfo Is Not Exist";
                return result;
            }
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    ef.Firereportout.Add(info);
                    ef.Entry<Firereportout>(info).State = EntityState.Added;
                    ef.SaveChanges();
                    var userInfo = ef.Userinfo.Where(p => p.UserId == info.UserId).FirstOrDefault();
                    //处置记录
                    Record record = new Record();
                    record.FireId = fireInfo.FireId;
                    record.HandleTime = info.TransDatetime;
                    record.Type = (int)RecordType.火灾报灭;
                    record.UserId = info.UserId;
                    record.Content = info.ReportOutContent;
                    record.AddTime = info.TransDatetime;
                    record.Ip = info.Ip;
                    record.Applicant = userInfo.RealName;
                   
                    record.CustomData = info.ReportOutId.ToString();
                    ef.Record.Add(record);
                    ef.Entry<Record>(record).State = EntityState.Added;


                    //消息
                    Message message = new Message();
                    message.FireId = fireInfo.FireId;
                    message.Type = (int)MessageType.报灭;
                    message.Content = string.Format(FireInfoTool.GetFireInfoMessageFormat(message.Type), fireInfo.FireName);
                    message.SendType = (int)MessageSendType.下级向上级发送;
                    message.IsRead = 0;
                    message.HandleTime = info.TransDatetime;
                    message.SendUserId = info.UserId;
                    message.Ip = fireInfo.Ip;
                    ef.Message.Add(message);
                    ef.Entry<Message>(message).State = EntityState.Added;

                    //过期等待
                    int nextValue =120;
                    var continueTime = info.TransDatetime.AddMinutes(nextValue).ToString("yyyy年MM月dd日HH时mm分");
                    Fireexpire fireexpire = new Fireexpire();
                    fireexpire.StartTime = info.TransDatetime;
                    fireexpire.Value = nextValue;
                    fireexpire.FireId = fireInfo.FireId;
                    fireexpire.MessageType = (int)MessageType.终报未作提示;
                    fireexpire.RecordType =(int)RecordType.终报未按时操作;
                    fireexpire.MessageContent = message.Content;
                    ef.Fireexpire.Add(fireexpire);
                    ef.Entry<Fireexpire>(fireexpire).State = EntityState.Added;
                    ef.SaveChanges();
                    trans.Complete();

                    #region 装载消息
                    List<Message> messages = new List<Message>() { message };
                    result.FirstParam = MessageBLL.Instance.TransMessageToFireMQTTResponseModel(messages, record.Type.Value);
                    #endregion
                    return result;
                }
            }
        }
    }
}
